package com.luoran.bartalks.service.impl;

import java.util.List;

import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.luoran.bartalks.core.UserContext;
import com.luoran.bartalks.dao.IBtPlanDetailsDao;
import com.luoran.bartalks.dao.IBtProjectDao;
import com.luoran.bartalks.dao.IBtProjectMemberDao;
import com.luoran.bartalks.dao.IUserDao;
import com.luoran.bartalks.entity.BaseInfo;
import com.luoran.bartalks.entity.biz.BtPlanDetails;
import com.luoran.bartalks.entity.biz.BtProject;
import com.luoran.bartalks.entity.biz.BtProjectMember;
import com.luoran.bartalks.entity.biz.UserInfo;
import com.luoran.bartalks.service.IBtProjectService;


@Service
public class BtProjectService implements IBtProjectService {

	@Autowired
	private IBtProjectDao projectDao;
	
	@Autowired
	private IUserDao userDao;
	
	@Autowired
	private IBtProjectMemberDao memberDao;
	
	@Autowired
	private IBtPlanDetailsDao planDetailsDao;

	@Override
	public PageQuery<? extends BaseInfo> getQueryList(PageQuery<? extends BaseInfo> pageQuery) {
		projectDao.queryProjectPage(pageQuery);
		return pageQuery;
	}


	//增加新项目
	@Transactional
	@Override
	public String addProject(BtProject project) {
		
		//新建项目
		projectDao.insertTemplate(project);
		
		//增加新项目的时候需要把项目成员和项目管理员增加到成员项目关系表
		String managerIds = project.getManagerId();//获取管理员id数组字符串
		String[] managerIdArr = managerIds.split(",");
		for (String memberId : managerIdArr) {
			BtProjectMember member =new BtProjectMember(); 
			//根据memberId(userId)查询并添加MemberProject
			UserInfo userInfo = userDao.getInfo(memberId);
			member.setCompanyId(userInfo.getCompanyId());
			member.setMemberId(userInfo.getId());//
			member.setMemberName(userInfo.getNick());//用户昵称设置为名称
			member.setProjectId(project.getId());//设置项目id
			member.setMemberType(0);//管理员
			memberDao.insertTemplate(member);
		}
		
		String memberIds = project.getMemberId();//获取项目成员id数组字符串
		String[] memberIdArr = memberIds.split(",");
		for (String memberId : memberIdArr) {
			BtProjectMember member =new BtProjectMember(); 
			//根据memberId(userId)查询并添加MemberProject
			UserInfo userInfo = userDao.getInfo(memberId);
			member.setCompanyId(userInfo.getCompanyId());
			member.setMemberId(userInfo.getId());
			member.setMemberName(userInfo.getNick());
			member.setProjectId(project.getId());
			member.setMemberType(1);//成员
			memberDao.insertTemplate(member);
		}
		
		return project.getId();
	}


	//删除项目
	@Override
	public int deleProject(BtProject project) {
		return projectDao.updateTemplateById(project);
	}


	//根据主键查询
	@Override
	public BtProject getProjectInfo(String id) {
		return projectDao.single(id);
	}


	//编辑项目
	@Transactional
	@Override
	public int updateProject(BtProject project) {
		
		//编辑项目成员信息
		//先删除指定公司项目下的项目成员关系
		memberDao.deleteProjectMemberByCompanyIdAndProjectId(UserContext.get().getCompanyId(),project.getId());
		
		//增加新项目的时候需要把项目成员和项目管理员增加到成员项目关系表
		String managerIds = project.getManagerId();//获取管理员id数组字符串
		String[] managerIdArr = managerIds.split(",");
		for (String memberId : managerIdArr) {
			BtProjectMember member =new BtProjectMember(); 
			//根据memberId(userId)查询并添加MemberProject
			UserInfo userInfo = userDao.getInfo(memberId);
			member.setCompanyId(userInfo.getCompanyId());
			member.setMemberId(userInfo.getId());
			member.setMemberName(userInfo.getNick());
			member.setProjectId(project.getId());
			member.setMemberType(0);
			memberDao.insertTemplate(member);
		}
		
		String memberIds = project.getMemberId();//获取项目成员id数组字符串
		String[] memberIdArr = memberIds.split(",");
		for (String memberId : memberIdArr) {
			BtProjectMember member =new BtProjectMember(); 
			//根据memberId(userId)查询并添加MemberProject
			UserInfo userInfo = userDao.getInfo(memberId);
			member.setCompanyId(userInfo.getCompanyId());
			member.setMemberId(userInfo.getId());
			member.setMemberName(userInfo.getNick());
			member.setProjectId(project.getId());
			member.setMemberType(1);
			memberDao.insertTemplate(member);
			
		}
		//更新项目信息
		return projectDao.updateTemplateById(project);
	}


	//项目启动,暂停
	@Override
	public int startProject(BtProject project) {
		//根据项目id,获取项目下所有的计划.
		List<BtPlanDetails> plans = planDetailsDao.findPlanByProjectId(project.getId());
		for (BtPlanDetails prosistentPlan : plans) {
			BtPlanDetails plan = new BtPlanDetails();
			plan.setId(prosistentPlan.getId());
			plan.setStarted(1);
			int count = planDetailsDao.updateTemplateById(plan);
			
		}
		return projectDao.updateTemplateById(project);
	}


	@Override
	public int updateTemplateById(BtProject project) {
		return projectDao.updateTemplateById(project);
	}

}
